package com.st.shopadmin.util;

import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateTimeUtil {
    public static  final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    //str--->>date
    public static Date strToDate(String dateTimeStr,String formatStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    //date---->>>str
    public static String dateToStr(Date date,String formatStr){
       if(date == null){
           return org.apache.commons.lang3.StringUtils.EMPTY;
       }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    //str--->>date
    public static Date strToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    //date---->>>str
    public static String dateToStr(Date date){
        if(date == null){
            return org.apache.commons.lang3.StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }
}
